// index.js
// 获取应用实例
const {
	calculateAge
} = require("../../utils/util")
const api = require("../../config/api")
const request = require("../../utils/request")
const app = getApp()
Component({
	properties: {
		patientList: Array,
		showModalStatus: Boolean,
	},
	data: {
		showModalStatus: false
	},

	ready: function() {},
	moved: function() {},
	detached: function() {},

	methods: {
		bindPatient: function() {
			app.globalData.createPatientEntrance = 4
			wx.navigateTo({
				url: '/pages/createPatient/select/select',
			})
			this.hideModal()
		},
		hideModal() {
			this.triggerEvent("bindChangeModel", false)
		},
		// // 选择患者
		// choosePatient(val) {
		// 	const patientInfo = val.currentTarget.dataset.item
		// 	// patientInfo.age = calculateAge(patientInfo.birthday)
		// 	this.triggerEvent("bindChangePatient", patientInfo)
		// 	this.setData({
		// 		showModalStatus: false
		// 	})
		// 	wx.setStorageSync('patientInfo', patientInfo)
		// },
		// 设置默认患者
		choosePatient: async function(val) {
			const userId = wx.getStorageSync('userId')
			const currentPatientInfo = wx.getStorageSync('patientInfo')
			const { id, patientName } = currentPatientInfo
			let patientInfo = val.currentTarget.dataset.item
			
			if(id===patientInfo.id){
				wx.showToast({
					icon:'none',
					title: patientInfo.patientName + '已经是当前孩子',
				})
				return
			}

			const data = {
				patientId: patientInfo.id,
				userId: userId
			}
			const res = await request.post(api.setDefault, data)
			if (res.httpStatus === 200) {
				app.getPatientList()

				patientInfo.isDefault = 1
				this.triggerEvent("bindChangePatient", patientInfo)
				this.setData({
					showModalStatus: false
				})
			}
		},
	}

})
